import VueRouter from "vue-router";
import Vue from "vue";
// 1. 先注册vuerouter插件
Vue.use(VueRouter);
// 2. 定义好映射关系
// 什么路径path,就是我们所谓的地址要对应什么组件
const routes = [
  {
    path: "/login",
    component: () => import("../pages/login.vue"),
  },
  {
    path: "/",
    component: () => import("../layout/index.vue"),
    // 重定向
    redirect: () => {
      //判断他是不是登录
      // localStorage  本地存储
      let isLogin = localStorage.getItem("token");
      if (!isLogin) {
        return "/login";
      } else {
        return "/home";
      }
    },
    children: [
      {
        path: "/home",
        component: () => import("../pages/home.vue"),
      },
      {
        path: "/demo",
        component: () => import("../pages/demo.vue"),
      },
    ],
  },
];
// 3. 将映射关系给vue-router插件
export const router = new VueRouter({
  routes, // (缩写) 相当于 routes: routes
});
